<template>
  <div class="prove">
    <section class="info">
      <hgroup>资料信息</hgroup>
      <el-form :model="ruleForm"
               :rules="rules"
               ref="ruleForm"
               label-width="100px"
               class="yuyi-ruleForm">
        <el-form-item label="资料类型" prop="name">
          <el-radio-group v-model="ruleForm.dataType">
            <el-radio v-for="(item, index) in dataTypeDic"
                      :key="index"
                      :label="item.dictValue">{{item.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="资料内容说明" prop="dataExplain">
          <el-input type="textarea" v-model="ruleForm.dataExplain"></el-input>
        </el-form-item>
        <el-form-item label="选择上传资料" prop="name">
          <el-upload
            class="upload-demo"
            ref="upload"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :show-file-list="false"
            multiple
            :auto-upload="false">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          </el-upload>

        </el-form-item>
        <el-form-item style="text-align: right;">
          <el-button type="primary" :loading="btnLoading" @click="submitForm('ruleForm')">
            确定{{isShowReady ? '修改' : ''}}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="img-box-list">

      </div>

    </section>

    <section class="list-box">
      <div class="fun-btn">
        <el-button size="small" type="primary" @click="handleAddEmpty">新增</el-button>
      </div>
      <el-table
        :data="tableData.list"
        border
        v-loading="tableLoading"
        height="calc(100vh - 200px)"
        class="table">
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="60">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="dataType"
          label="资料类型">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="dataExplain"
          label="资料说明">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="filePath"
          label="资料存储路径">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="gmtModified"
          label="上传日期">
        </el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          label="操作"
          width="130">
          <template slot-scope="scope">
            <el-button @click="handleDel(scope.row)" type="text" style="color: #F56C6C">删除</el-button>
            <el-button @click="handleEdit(scope.row)" type="text">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 40, 60, 80]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="Number(tableData.total)">
      </el-pagination>
    </section>

  </div>
</template>

<script>
import {
  addDjProveList,
  detDjProveList,
  editDjProveList,
  getDjProveList
} from '../../../../api/utilize/utilizeDjProve'
import { getArchivesDicId } from '../../../../api/dictionary/dictionary'

export default {
  name: 'proveMaterial',
  props: {
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      fileList: [],
      tableLoading: true,
      btnLoading: false,
      ruleForm: {
        dataType: '',
        dataExplain: '',
        filePath: '图片路径' // 图片路径
      },
      rules: {},
      isShowReady: false, // 编辑
      page: {
        pageNo: 1,
        pageSize: 1000
      },
      searchData: {
        val: '',
        select: 'username'
      },
      params: [],
      tableData: {},
      dataTypeDic: [] // 资料类型字典
    }
  },
  mounted () {
    this.getDic()
    this.getList()
  },
  watch: {
    rowData () {
      this.getList()
      this.handleAddEmpty()
    }
  },
  methods: {
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePreview (file) {
      console.log(file)
    },
    getDic () {
      getArchivesDicId('data_type').then(res => {
        // 资料类型
        this.dataTypeDic = res.data.data
        this.ruleForm.dataType = this.dataTypeDic[0].dictValue
      })
    },
    getList () {
      this.tableLoading = true
      const param = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        params: [{
          name: 'idUseRegister',
          op: 'eq',
          value: this.rowData.id
        }]
      }
      if (this.params.length > 0) {
        param.params = param.params.concat(this.params)
      }
      getDjProveList(param).then(res => {
        this.tableData = res.data.data
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.page.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.page.pageNo = val
      this.getList()
    },
    handleAddEmpty () {
      // 新增时清空表单
      this.isShowReady = false
      this.ruleForm.dataExplain = '' // 资料说明
    },
    handleEdit (row) {
      // 编辑
      this.isShowReady = true
      Object.keys(row).forEach(key => {
        this.ruleForm[key] = row[key]
      })
    },
    submitForm (formName, type) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          if (this.isShowReady) {
            // 编辑修改
            editDjProveList(this.ruleForm).then(() => {
              this.$message.success('修改成功！')
              this.btnLoading = false
              this.isShowReady = false
              this.handleAddEmpty()
              this.getList()
            }).catch(() => {
              this.btnLoading = false
            })
          } else {
            // 新增
            delete this.ruleForm.id
            this.ruleForm.idUseRegister = this.rowData.id
            addDjProveList(this.ruleForm).then(() => {
              this.$message.success('新增成功！')
              this.btnLoading = false
              this.handleAddEmpty()
              this.getList()
            }).catch(() => {
              this.btnLoading = false
            })
          }
        } else {
          return false
        }
      })
    },
    handleDel (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        detDjProveList([row.id]).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  @import "../../../../styles/mixin";

  .prove {
    padding: 10px 20px;
    background: #ccc;
    height: 100%;
    @include fj;

    .info {
      width: 520px;
      background: #fff;
      padding: 0 15px;
      border-radius: 5px;

      hgroup {
        @include sc(16px, #333);
        line-height: 30px;
        margin-bottom: 25px;
      }

      ::v-deep .el-radio {
        margin: 5px 0;
        & + .el-radio{
          margin-left: 15px;
        }
      }

      ::v-deep .el-upload-list {
        display: none;
      }
    }

    .list-box {
      background: #fff;
      width: calc(100% - 535px);
      .fun-btn{
        padding: 15px 0 0 15px;
      }
    }

    .table {
      ::v-deep th, ::v-deep td {
        text-align: center;
      }
    }
  }
</style>
